package com.lingkang.xcoa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lingkang.xcoa.data.dao.OaRoleMapper;
import com.lingkang.xcoa.data.dao.OaUserMapper;
import com.lingkang.xcoa.data.dao.OaUserRoleMapper;
import com.lingkang.xcoa.data.dto.ResponsePageResult;
import com.lingkang.xcoa.data.dto.ResponseResult;
import com.lingkang.xcoa.data.vo.PeopleListVo;
import com.lingkang.xcoa.data.vo.RoleUserListVo;
import com.lingkang.xcoa.service.AdminService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author linke
 * @date 2020/8/22 23:19
 * @description
 */
@Service
public class AdminServiceImpl implements AdminService {

  @Autowired
  private OaUserMapper oaUserMapper;
  @Autowired
  private OaRoleMapper oaRoleMapper;
  @Autowired
  private OaUserRoleMapper oaUserRoleMapper;

  @Override
  public ResponsePageResult getPeopleList(String username, String nameLike, Integer page,
      Integer size) {
    if (page == null) {
      page = 1;
    }
    if (size == null) {
      size = 10;
    }
    if (size > 50) {
      size = 50;
    }
    Page pages = PageHelper.startPage(page, size);
    List<PeopleListVo> peopleList = oaUserMapper.getPeopleList(username, nameLike);
    return new ResponsePageResult(0, page, size, pages.getTotal(), peopleList);
  }

  @Override
  public List<RoleUserListVo> getRoleList(String username) {
    return oaRoleMapper.getList(username);
  }

  @Override
  @Transactional("mybatisTransactionManager")
  public ResponseResult updateInfo(String username, List<Integer> roles, Integer status) {
    oaUserMapper.updateStatus(username,status==1?1:0);
    oaUserRoleMapper.deleteRoleByUsername(username);
    oaUserRoleMapper.insertManyRole(username,roles);
    return new ResponseResult(0,"操作成功！");
  }
}
